package com.smarttest.portal.controller;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarttest.common.beans.JsonDataResult;
import com.smarttest.portal.pojo.RuleParam;
import com.smarttest.portal.service.RuleParamService;
import com.smarttest.portal.service.RuleService;

@Controller
@RequestMapping("/rule/param")
public class RuleParamController {
	@Autowired
	RuleParamService ruleParamService;
	@Autowired
	RuleService ruleService;
	
	@RequestMapping("/query/{ruleIndex}-{ruleType}.action")
	@ResponseBody
	public JsonDataResult getRule(@PathVariable String ruleIndex,@PathVariable String ruleType) {
		JsonDataResult result = ruleService.getRuleByParameter(ruleIndex,ruleType);
		return result;
	}
	
	@RequestMapping("/save/{ruleType}.action")
	@ResponseBody
	public JsonDataResult insertItemParam(@PathVariable String ruleType, String paramData) {
		//创建pojo对象
		RuleParam ruleParam = new RuleParam();
		ruleParam.setRuleType(ruleType);
		ruleParam.setParamData(paramData);
		JsonDataResult result = ruleParamService.insertRuleParam(ruleParam);
		return result;
	}
	
	@RequestMapping("/init/menu.action")
	@ResponseBody
	public JsonDataResult initRuleMenu(String index) {
		JsonDataResult result = ruleService.getRuleByParameter(index,"");
		return result;
	}
	
	@RequestMapping("/init/ruleIndexies.action")
	@ResponseBody
	public JsonDataResult initRuleIndexies() {
		Set<String>indexSet = ruleParamService.getRuleIndexes();
		if(indexSet!=null&&indexSet.size()>0){
				return JsonDataResult.ok(indexSet);
		}
		return JsonDataResult.ok();
	}
	
	@RequestMapping("/init/rulePatterns.action")
	@ResponseBody
	public JsonDataResult initRulePatterns() {
		JsonDataResult result = ruleParamService.getRuleTypes();
		return result;
	}
	
	@RequestMapping(value="/update.action",method=RequestMethod.POST)
	@ResponseBody
	public JsonDataResult saveRules(String ruleIndex,String ruleName,String ruleBody,String modifiedRuleName) {
		JsonDataResult result = ruleService.updateRule(ruleIndex, ruleName,ruleBody,modifiedRuleName);
		return result;
	}
	
	
	@RequestMapping(value="/create.action",method=RequestMethod.POST)
	@ResponseBody
	public JsonDataResult addRules(String ruleIndex,String ruleName,String ruleType) {
		JsonDataResult result = ruleService.createRule(ruleIndex, ruleName, ruleType);
		return result;
	}
}
